
import { _decorator, Component, Node, Sprite, UITransform, Animation, animation, AnimationClip, Vec3, SpriteFrame } from 'cc'
import { EntityManager } from '../../Base/EntityManager'
import { ENTITY_TYPE_ENUM, DIRECTION_ENUM, ENTITY_STATE_ENUM, EVENT_ENUM } from '../../Enums'
import EventManager from '../../Runtime/EventManager'
import DataManager from '../../Runtime/DataManager'
import { DoorStateMachine } from './DoorStateMachine'
import { IEntity } from '../../Levels'
const { ccclass, property } = _decorator


@ccclass('DoorManager')
export class DoorManager extends EntityManager {
    async init(params: IEntity) {
        this.fsm = this.addComponent(DoorStateMachine)
        await this.fsm.init()
        super.init(params)

        // 玩家移动结束后，要调整木骷髅的方向以朝向玩家
        EventManager.Instance.on(EVENT_ENUM.DOOR_OPEN, this.onOpen, this)
    }

    onDestroy() {
        super.onDestroy()
        EventManager.Instance.off(EVENT_ENUM.DOOR_OPEN, this.onOpen)
    }

    onOpen() {
        // 当所有敌人都死亡时才开门
        if (
            DataManager.Instance.enemies.every((enemy: EntityManager) => enemy.state === ENTITY_STATE_ENUM.DEATH) &&
            this.state !== ENTITY_STATE_ENUM.DEATH
        ) {
            this.state = ENTITY_STATE_ENUM.DEATH
        }
    }
}
